package obs.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import obs.bean.User;
import obs.dao.UserDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.ibatis.sqlmap.client.SqlMapClient;

@Component
public class UserDaoImpl implements UserDao {

    @Autowired
    private SqlMapClient sqlMapClient;

    @Override
    public boolean login(String uid, String pwd) throws SQLException {
        if (uid == null || pwd == null) {
            return false;
        }
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", uid);
        params.put("password", pwd);
        User user = (User) sqlMapClient.queryForObject("login", params);
        if (user != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean regist(String uid, String pwd, Date bothday) throws SQLException {
        if (StringUtils.isEmpty(uid) || StringUtils.isEmpty(pwd)) {
            return false;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("username", uid);
        params.put("password", pwd);
        params.put("bothday", bothday);
        sqlMapClient.insert("insert", params);
        return true;
    }

    @Override
    public List<User> get(String uid) throws SQLException {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("username", "%" + uid + "%");
        @SuppressWarnings("unchecked")
        List<User> users = sqlMapClient.queryForList("getUsers", params);
        return users;
    }
}
